package pishen;

import java.util.ArrayList;
import java.util.List;

import edu.uci.ics.jung.graph.Graph;

public class ICSimulator {
	private Graph<Node, Edge> graph;
	private List<Node> infectedList;
	private List<Node> newestActiveList;
	private List<Node> newestActiveNeighborList;
	
	public ICSimulator(Graph<Node, Edge> graph, List<Node> sourceList){
		this.graph = graph;
		this.infectedList = sourceList;
		newestActiveList = new ArrayList<Node>();
		for(Node node: infectedList){
			newestActiveList.add(node);
		}
		newestActiveNeighborList = new ArrayList<Node>();
	}
	
	public void startSimulate(){
		int step = 0;
		while(newestActiveList.size() > 0){
			System.out.println("step=" + step + " node infected=" + infectedList.size());
			step++;
			for(Node node: newestActiveList){
				for(Node neighbor: graph.getSuccessors(node)){ // **Directed Graph**
					if(!infectedList.contains(neighbor)){
						if(Math.random() < 0.8){
							//infect the neighbor
							newestActiveNeighborList.add(neighbor);
							infectedList.add(neighbor);
						}
					}
				}
			}
			newestActiveList.clear();
			for(Node node: newestActiveNeighborList){
				newestActiveList.add(node);
			}
			newestActiveNeighborList.clear();
		}
	}
	
	public List<Node> getInfectedList(){
		return infectedList;
	}
}
